#include "DbPart.h"
#include "connectpool.h"
#include <sstream>
#include <vector>
#include "Game.h"
#include "Logic.h"


CDbPart::CDbPart():m_dbHandler(NULL)
{
}

CDbPart::~CDbPart()
{
}

bool CDbPart::createFlowTable(const std::string& tname)
{
	ostringstream ossSQL;
	ossSQL << "CREATE TABLE IF NOT EXISTS `"<< tname << "`(`seq` BIGINT UNSIGNED NOT NULL auto_increment,`uid` INT UNSIGNED NOT NULL DEFAULT 0,`act` INT UNSIGNED NOT NULL DEFAULT 0, `delt` BIGINT NOT NULL DEFAULT 0, `optime` BIGINT UNSIGNED NOT NULL DEFAULT 0, `balance` BIGINT NOT NULL DEFAULT 0, `cycle` BIGINT UNSIGNED NOT NULL DEFAULT 0, `orderid` BIGINT UNSIGNED NOT NULL DEFAULT 0, `associatedorder` BIGINT UNSIGNED NOT NULL DEFAULT 0, `agencyid` INT UNSIGNED NOT NULL DEFAULT 0, `gameid` INT UNSIGNED NOT NULL DEFAULT 0, `provider` VARCHAR(20) NOT NULL DEFAULT \'\',`ext` VARCHAR(128) NOT NULL DEFAULT \'\',`errcode` INT NOT NULL DEFAULT 0, PRIMARY KEY (`seq`), INDEX idx_uid (`uid`),INDEX idx_act (`act`),INDEX idx_optime (`optime`),INDEX idx_errcode (`errcode`),INDEX idx_agencyid (`agencyid`),INDEX idx_gameid (`gameid`)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE utf8mb4_bin";

	if (!m_dbHandler->UpdateSql(ossSQL.str().c_str()))
	{
		m_dbHandler->m_strError = m_dbHandler->m_pMysqlConn->What();
		log_error("createFlowTable err: %s\r sql=%s \n", m_dbHandler->m_pMysqlConn->What().c_str(), ossSQL.str().c_str());

		return false;
	}

	return true;
}

bool CDbPart::addRecord(const std::string& tname, const unsigned int uid, const unsigned int act, const int64 delt, const _u64_ tt, const int64 balance, const _u64_ cycle, const _u64_ orderid, const _u64_ associatedorder, const unsigned int agencyid, const unsigned int gameid, const std::string& provider, const std::string& ext, const int errcode)
{
	ostringstream ossSQL;
	ossSQL << "insert into " << tname << "(uid, act, delt, optime, balance, cycle, orderid, associatedorder, agencyid, gameid, provider, ext, errcode) values(" << uid << "," << act << "," << delt << "," << tt << "," << balance << "," << cycle << "," << orderid << "," << associatedorder << "," << agencyid << "," << gameid << ",\'" << provider << "\',\'" << ext << "\',"<< errcode <<")";

	if (!m_dbHandler->UpdateSql(ossSQL.str().c_str()))
	{
		m_dbHandler->m_strError = m_dbHandler->m_pMysqlConn->What();
		log_error("addRecord err: %s\r sql=%s \n", m_dbHandler->m_pMysqlConn->What().c_str(), ossSQL.str().c_str());

		return false;
	}

	return true;
}
